package com.pat.jrgwt.client;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DisclosurePanel;

public class JrcEventsLog extends Composite
{

    private static JrcEventsLogUiBinder uiBinder = GWT.create(JrcEventsLogUiBinder.class);
    private final ListBox lstLogs = new ListBox();
    private ArrayList<String> arLogs = new ArrayList<String>();
    private static JrcEventsLog thisInstance = null;
    private int logsToShow = 30;

    @UiField DisclosurePanel EventsLog;

    interface JrcEventsLogUiBinder extends UiBinder<Widget, JrcEventsLog>
    {
    }

    public static JrcEventsLog getInstance()
    {
        if( thisInstance == null )
        {
            thisInstance = new JrcEventsLog();
        }
        
        return thisInstance;
    }
    
    public JrcEventsLog()
    {
        initWidget(uiBinder.createAndBindUi(this));
        lstLogs.setHeight("200px");
        lstLogs.setWidth("890px");
        lstLogs.setVisibleItemCount(logsToShow);
        lstLogs.setEnabled(false);
        
//        EventsLog.setHeight("500px");
        EventsLog.add(lstLogs);
    }
    
    public void add(String log)
    {
        arLogs.add(log);
        refreshLogs();
    }
    
    public void refreshLogs()
    {
//        Window.alert("Size of events log: " + arLogs.size());
        int total = arLogs.size() > logsToShow ? logsToShow : arLogs.size();
        lstLogs.clear();
        for (int i = 1; i <= total; i++)
        {
            lstLogs.addItem(arLogs.get(arLogs.size() - i));
        }
    }
}
